package cloneExample;

import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws CloneNotSupportedException {
		People[] peoples = {
				new People(1, "java", new Address(110, "北京"))
		};
		ArraysCopy(peoples);
		cloneTest();
	}

	// 深拷贝 -- 克隆
	private static void cloneTest() throws CloneNotSupportedException {
		Address address = new Address(110, "北京");
		People people1 = new People(1, "Java", address);
		// clone出来一个对象 people2
		People people2 = (People) people1.clone();
		people1.getAddress().setCity("西安");
		System.out.println("p1:" + people1.getAddress().getCity()); //西安
		System.out.println("p2:" + people2.getAddress().getCity()); //北京
		// 一个是西安，一个是北京，说明了克隆出来的对象是不同的。
	}

	private static void ArraysCopy(People[] people) {
		People[] peopleCloned = Arrays.copyOf(people, people.length);
		peopleCloned[0].setName("JDK");
		System.out.println(people[0].getName()); //jdk
		System.out.println(peopleCloned[0].getName());//jdk
		// 修改了一个clone之后的对象，结果之前的数据也被修改掉了。这就是复制的不够深。
	}
}
